﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace physio.assist
{
    /// <summary>
    /// Interaction logic for Noticias.xaml
    /// </summary>
    public partial class Noticias : Window
    {
        public Noticias()
        {
            InitializeComponent();




            //Ajusta posição e tamanho das janelas
            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;

            winNoticias.Width = width;
            winNoticias.Height = height;

            winNoticias.Left = 0;
            winNoticias.Top = 0;



            //Ajusta posicionamento e tamanho dos panels
            pnlGeral.Width = width;
            pnlGeral.Height = height;

            pnlVoltar.Height = height;
            pnlPrimaria.Height = height;
            pnlSecundaria.Height = height;

            pnlVoltar.Width = width * 0.2;
            pnlPrimaria.Width = width * 0.5;
            pnlSecundaria.Width = width * 0.3;

            pnlSecundaria.Margin = new Thickness(30, 0, 0, 0);



            //Lista notícias mais recentes
            ListaNoticias();

        }




        //NOTICIAS
        private void ListaNoticias()
        {

            PhysioAssistWS.PhysioAssistSoapClient ws = new PhysioAssistWS.PhysioAssistSoapClient();

            foreach (PhysioAssistWS.Noticia noticia in ws.ListaNoticia())
            {

                InsereNoticia(noticia);

            }

            foreach (PhysioAssistWS.Noticia noticia in ws.ListaNoticia())
            {

                CarregaNoticia(noticia.Id);
                break;

            }

        }
        private void InsereNoticia(PhysioAssistWS.Noticia noticia)
        {

            //Cria o panel
            StackPanel pnl = new StackPanel();
            pnl.Orientation = Orientation.Horizontal;
            pnl.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            //Cria label da data
            Label lblData = new Label();
            lblData.Content = noticia.Data.ToShortDateString() + " - " + noticia.Fonte;
            lblData.FontSize = 20;
            lblData.Foreground = Brushes.Silver;

            //Cria label do titulo
            StackPanel pnl2 = new StackPanel();
            Label lblTitulo = new Label();
            TextBlock txt = new TextBlock();
            txt.Text = noticia.Titulo + "\n";
            txt.TextWrapping = TextWrapping.Wrap;
            lblTitulo.Content = txt;
            lblTitulo.FontSize = 25;
            lblTitulo.Foreground = Brushes.White;

            //Insere componente no panel
            pnl.Children.Add(lblData);
            pnl2.Children.Add(lblTitulo);

            //Insere panel no panel de aniversarios
            pnlSecundaria.Children.Add(pnl);
            pnlSecundaria.Children.Add(pnl2);

        }
        private void CarregaNoticia(int id)
        {

            PhysioAssistWS.PhysioAssistSoapClient ws = new PhysioAssistWS.PhysioAssistSoapClient();
            PhysioAssistWS.Noticia noticia = ws.GetNoticia(id);

            //Titulo
            TextBlock txt = new TextBlock();
            txt.Text = noticia.Titulo;
            txt.TextWrapping = TextWrapping.Wrap;
            lblTitulo.Content = txt;

            //Data
            lblData.Content = noticia.Data;

            //Fonte
            lblFonte.Content = noticia.Fonte;

            //Conteudo
            txt = new TextBlock();
            txt.Text = noticia.Conteudo;
            txt.TextWrapping = TextWrapping.Wrap;
            lblConteudo.Content = txt;



        }







        //RECONHECIMENTO DE VOZ
        private Microsoft.Speech.Recognition.SpeechRecognitionEngine speechEngine;
        private Microsoft.Kinect.KinectSensor sensor;
        private void winAgenda_Loaded(object sender, RoutedEventArgs e)
        {


            //Obtem o sensor ativo
            foreach (var potentialSensor in Microsoft.Kinect.KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == Microsoft.Kinect.KinectStatus.Connected)
                {
                    sensor = potentialSensor;
                    break;
                }
            }


            if (null != sensor)
            {

                try
                {
                    sensor.Start();
                }
                catch (System.IO.IOException)
                {
                    sensor = null;
                }
            }








            //RECONHECIMENTO DE VOZ
            Microsoft.Speech.Recognition.RecognizerInfo ri = GetKinectRecognizer();

            if (null != ri)
            {

                this.speechEngine = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(ri.Id);

                // Create a grammar from grammar definition XML file.
                using (var memoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(Properties.Resources.PhysioAssistGrammar)))
                {
                    var g = new Microsoft.Speech.Recognition.Grammar(memoryStream);
                    speechEngine.LoadGrammar(g);
                }

                speechEngine.SpeechRecognized += SpeechRecognized;

                speechEngine.SetInputToAudioStream(sensor.AudioSource.Start(), new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(Microsoft.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
                speechEngine.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple);
            }


        }
        private static Microsoft.Speech.Recognition.RecognizerInfo GetKinectRecognizer()
        {
            foreach (Microsoft.Speech.Recognition.RecognizerInfo recognizer in Microsoft.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers())
            {
                string value;
                recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return recognizer;
                }
            }

            return null;
        }
        private void SpeechRecognized(object sender, Microsoft.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            // Speech utterance confidence below which we treat speech as if it hadn't been heard
            const double ConfidenceThreshold = 0.3;

            if (e.Result.Confidence >= ConfidenceThreshold)
            {
                switch (e.Result.Semantics.Value.ToString())
                {
                    case "CLOSE":
                        winNoticias.Close();
                        break;

                    case "FECHAR":
                        winNoticias.Close();
                        break;
                }
            }
        }










    }
}
